import GameConfig from "../../GameConfig";
import GameCfgData from "../../lib/GameCfgData";
import Sdk from "../../lib/Sdk";
import {ui} from "../../ui/layaMaxUI"
import PlayerData from "../data/PlayerData";
export default class LoadScene extends ui.LoadSceneUI{
    onEnable(): void {
        this.height=this.bg.height=Laya.stage.height;
        this.loadSubPack();
    }

    private loadSubPack(){
		if(Laya.Browser.onMiniGame){
			let self=this;
			//Sdk.getIns().showWeixinLoading();
			const loadTask = Laya.Browser.window.wx.loadSubpackage({
				name: 'bao1', // name 可以填 name 或者 root
				success: function(res) {
					self.loadCfg();
				},
				fail: function(res) {
				  // 分包加载失败通过 fail 回调
				}
			  })

			  loadTask.onProgressUpdate(res=>{
				
			  });
		}else{
			this.loadCfg();
		}	
	}


	private loadCfg(){
		Laya.loader.load("res/gameEleCfg.json", Laya.Handler.create(this, this.onCfgLoaded),  Laya.Handler.create(this, this.laodPross), Laya.Loader.JSON);
		this.loadOpenData();
	}

	
    private laodPross(value:number){
		let tptoss = Math.floor(value*100);
        this.prossTxt.text=tptoss+"%";
    }
	
	private onCfgLoaded(data){
		//console.log(data);
		GameCfgData.getIns().init(data);
		this.loadData();
	}
	
	private loadData(){
		//Sdk.getIns().removeStorage("alchad1");
		
		Sdk.getIns().getStorage((data)=>{
			if(data)PlayerData.getIns().initData(data);
			GameCfgData.getIns().loadGateCfgDate(PlayerData.getIns().currGate,this.onGateComplete);
		});
	}

	private onGateComplete(){
		Laya.Scene.open("StartScene.scene",true);
	}
	
	private loadOpenData(){
		if(Laya.Browser.onMiniGame){
			//加载一个json和图集
			//console.log("loadOpenData");
			Laya.loader.load(["res/atlas/comp.atlas"],Laya.Handler.create(null,function(){
				//加载完成
				//使用接口将图集透传到子域
				//Laya.MiniAdpter.sendJsonDataToDataContext("res/atlas/test.json");
				Laya.MiniAdpter.sendAtlasToOpenDataContext("res/atlas/comp.atlas");    
				//console.log("Laya.MiniAdpter.sendAtlasToOpenDataContext");
				//使用接口将json投促函到子域
			}));
		}
	}
}